customSchema = class({})

function customSchema:init()

    -- Check the schema_examples folder for different implementations

    -- Flag Example
    -- statCollection:setFlags({version = GetVersion()})

    -- Listen for changes in the current state
    ListenToGameEvent('game_rules_state_change', function(keys)
        local state = GameRules:State_Get()

        -- Send custom stats when the game ends
        if state == DOTA_GAMERULES_STATE_POST_GAME then

            -- Build game array
            local game = BuildGameArray()

            -- Build players array
            local players = BuildPlayersArray()

            -- Print the schema data to the console
            if statCollection.TESTING then
                PrintSchema(game,players)
            end

            -- Send custom stats
            if statCollection.HAS_SCHEMA then
                statCollection:sendCustom({game=game, players=players})
            end
        end
    end, nil)
end

-------------------------------------

-- In the statcollection/lib/utilities.lua, you'll find many useful functions to build your schema.
-- You are also encouraged to call your custom mod-specific functions

-- Returns a table with our custom game tracking.
function BuildGameArray()
    local game = {}

    -- Add game values here as game.someValue = GetSomeGameValue()
	game.eh=storage:GetEmpGoldHist()
	game.wn=storage:getWinner()
	--game.th=storage:GetTideKillers()
	
    return game
end

-- Returns a table containing data for every player in the game
function BuildPlayersArray()
    local players = {}
    for playerID = 0, DOTA_MAX_PLAYERS do
        if PlayerResource:IsValidPlayerID(playerID) then
            if not PlayerResource:IsBroadcaster(playerID) then

                local hero = PlayerResource:GetSelectedHeroEntity(playerID)
                
                local teamname="North"
                if hero:GetTeamNumber()==DOTA_TEAM_GOODGUYS then
                	teamname="South"
                end
				
		local kickStatus="Active"
		if storage:GetDisconnectState(playerID)~=0 then
			kickStatus="Kicked"
		end

                table.insert(players, {
                    -- steamID32 required in here
                    steamID32 = PlayerResource:GetSteamAccountID(playerID),

                    -- Example functions for generic stats are defined in statcollection/lib/utilities.lua
                    -- Add player values here as someValue = GetSomePlayerValue(),
                    tm  = 	teamname,			--This hero's custom team name
                    shp = 	storage:GetHeroName(playerID), 	--This hero's custom name
                    kls = 	hero:GetKills(),   		--This hero's kills
                    dth = 	hero:GetDeaths(),  		--This hero's deaths
                    lvl = 	hero:GetLevel(),		--This hero's levels
                    afk = 	kickStatus,			--This hero's custom connection status
                    
                    -- Item List
                    bo=storage:GetPlayerHist(playerID),
                })
            end
        end
    end

    return players
end

-- Prints the custom schema, required to get an schemaID
function PrintSchema( gameArray, playerArray )
    print("-------- GAME DATA --------")
    DeepPrintTable(gameArray)
    print("\n-------- PLAYER DATA --------")
    DeepPrintTable(playerArray)
    print("-------------------------------------")
end

-- Write 'test_schema' on the console to test your current functions instead of having to end the game
if Convars:GetBool('developer') then
    Convars:RegisterCommand("test_schema", function() PrintSchema(BuildGameArray(),BuildPlayersArray()) end, "Test the custom schema arrays", 0)
end

-------------------------------------

-- If your gamemode is round-based, you can use statCollection:submitRound(bLastRound) at any point of your main game logic code to send a round
-- If you intend to send rounds, make sure your settings.kv has the 'HAS_ROUNDS' set to true. Each round will send the game and player arrays defined earlier
-- The round number is incremented internally, lastRound can be marked to notify that the game ended properly
function customSchema:submitRound(isLastRound)

    local winners = BuildRoundWinnerArray()
    local game = BuildGameArray()
    local players = BuildPlayersArray()

    statCollection:sendCustom({game=game, players=players})

    isLastRound = isLastRound or false --If the function is passed with no parameter, default to false.
    return {winners = winners, lastRound = isLastRound}
end

-- A list of players marking who won this round
function BuildRoundWinnerArray()
    local winners = {}
    local current_winner_team = GameRules.Winner or 0 --You'll need to provide your own way of determining which team won the round
    for playerID = 0, DOTA_MAX_PLAYERS do
        if PlayerResource:IsValidPlayerID(playerID) then
            if not PlayerResource:IsBroadcaster(playerID) then
                winners[PlayerResource:GetSteamAccountID(playerID)] = (PlayerResource:GetTeam(playerID) == current_winner_team) and 1 or 0
            end
        end
    end
    return winners
end

-------------------------------------
--          My Custom Functions    --
-------------------------------------

playerItemHist={}

function storage:AddToPlayerItemHist(pid, ic)

	if playerItemHist[pid]==nil then
		 playerItemHist[pid]=""
		 print("Created Array")
	end
	if ic~=nil then
		if string.len(tostring(playerItemHist[pid]))<96 then
		  playerItemHist[pid]=playerItemHist[pid] .. ic .. ","
	  end
	end
end

function storage:GetPlayerHist(playerID)
	if playerItemHist[playerID] ~= nil then
		return playerItemHist[playerID]
	end
	return "none"
end

DisconnectKicked={}

function storage:GetDisconnectState(playerID)
	print("getDisconnect")
	for _,hero in pairs( Entities:FindAllByClassname( "npc_dota_hero*")) do
			if hero ~= nil and hero:IsOwnedByAnyPlayer() then
				if hero:GetPlayerID() == playerID then
					if DisconnectKicked[hero]~= nil then
						return DisconnectKicked[hero]
						else
						return 0
					end
					
				end
			end
		end
		return 0
	end
	
	function storage:GetHeroName(playerID)
    for _,hero in pairs( Entities:FindAllByClassname( "npc_dota_hero*")) do
		if hero ~= nil and hero:IsOwnedByAnyPlayer() then
			if hero:GetPlayerID() == playerID then
				return name_lookup[hero:GetName()]
			end
		end
	end
	return "failed"
end
